#include <stdio.h>
#define MOD 1000000

int isPandigital(int n)
{
	int a[10]={0}, i=0;
	while(n) {
		a[n%10]++;
		if (a[n%10] != 1)return 0;
		n /=10;
	}
	while(i<10)if (a[i++] !=1)return 0;
	return 1;
}

int mod(int *s, int n)
{
	int r=0, x[2]={0}, org = n, diff=1;
	while (n<MOD) n *=10;
	n /=10;
	x[1] = n;
	do {
		diff = subtract(s, x);
		if (diff < 0)subtract(x, 1);
		if (diff > org)sum(s, 1);
	}while(diff)
}

void sum(int *s, int n)
{
	int s1;
	s[0] +=n;
	s1 = s[0]/MOD;
	s[0] %= MOD; 
	s[1] += s1;
}

void perm(int *a, int n)
{
	if (n==1)return *a;
	return perm(a, --n);
}

int allDistinct(int n)
{
	int a[10]={0}, i=0;
	while(n) {
		a[n%10]++;
		if (a[n%10] != 1)return 0;
		n /=10;
	}
	while(i<10)if (a[i++] >1)return 0;
	return 1;
}

int buildNext(int *a, int d)
{
	int i, d1=d, digits=0;
	while(d) {				
		digits++;
		a[d%10]++;
		d/=10;
	}
	i=0;
	while(a[i++]);
	return d1/10+(i-1)*100;
}


int main()
{
	int d17, d13, d11, d7, d5, d3, d2, d, i, j, digits;
	int a[10]={0}, s[2]={0}, primes[6]={13, 11, 7, 5, 3,2}, d[3] = {952867, 357289};
	for (i=0; i<2; i++) {
		
		if (allDistinct(first)) {			
			next = buildNext(a, first);					
			while (next%primes[k++] == 0 && k<6) {
				next = buildNext(a, next);
			}
			k = 0;
		}
		j = 0;
		while (j<10)a[j++]=0;
	}
}
